package com.example.hotelmanagement.controller;

import com.example.hotelmanagement.entity.Order;
import com.example.hotelmanagement.entity.Result;
import com.example.hotelmanagement.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping
    @ResponseBody
    public Result getAllOrders() {
        return Result.ok(orderService.findAll());
    }

    @GetMapping("/{id}")
    public Result getOrderById(@PathVariable Integer id) {
        Order order = orderService.findById(id);
        return order != null ? Result.ok(order) : Result.error();
    }

    @GetMapping("/reservation/{reservationId}")
    public Result getOrdersByReservation(@PathVariable Integer reservationId) {
        List<Order> orders = orderService.findByReservationId(reservationId);
        return Result.ok(orders);
    }

    @GetMapping("/status/{status}")
    public Result getOrdersByStatus(@PathVariable String status) {
        List<Order> orders = orderService.findByStatus(status);
        return Result.ok(orders);
    }

    @PostMapping
    public Result createOrder(@RequestBody Order order) {
        return Result.ok(orderService.save(order));
    }

    @PutMapping
    public Result updateOrder(@RequestBody Order order) {
        return Result.ok(orderService.update(order));
    }

    @DeleteMapping("/{id}")
    public Result deleteOrder(@PathVariable Integer id) {
        return Result.ok(orderService.delete(id));
    }
}